﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;
using System.Transactions;

using LINQtoSQLDataProvider;
using System.Data.SqlClient;

namespace Dragon.Prototype1.Service
{
    public class RoleService
    {
        public static List<usp_Role_GetUsersByRoleIdResult> GetUsersByRoleId(Guid roleid)
        {
            using(WMSDataContext db = new WMSDataContext())
            {
                return db.usp_Role_GetUsersByRoleId(roleid).ToList<usp_Role_GetUsersByRoleIdResult>();
            }
        }
        public static Role GetRole(Guid roleid)
        {
            using (WMSDataContext db = new WMSDataContext())
            {
                return db.Roles.Single(role => role.RoleId == roleid);                
            }            
        }

        public static bool RemoveUserFromRole(Guid userid, Guid roleid)
        {
            using (WMSDataContext db = new WMSDataContext())
            {
                try
                {
                    UserRoleRelationship userrole=db.UserRoleRelationships.Single(p => p.RoleId == roleid && p.UserId == userid);
                    db.UserRoleRelationships.DeleteOnSubmit(userrole);
                    db.SubmitChanges();
                    return true;
                }
                catch (ChangeConflictException)
                {
                    return false;
                }
            }
        }

        public static bool DeleteRole(Guid roleid)
        {
            using (WMSDataContext db = new WMSDataContext())
            {
                try
                {
                    Role role=db.Roles.Single(r => r.RoleId == roleid);
                    db.Roles.DeleteOnSubmit(role);
                    db.SubmitChanges();
                    return true;
                }
                catch (ChangeConflictException)
                {
                    return false;
                }
                catch (SqlException)
                {
                    return false;
                }
            }
        }

        public static List<usp_Role_AddUsersToRoleResult> AddUsersToRole(Guid roleid, Guid[] userids)
        {
            StringBuilder sb= new StringBuilder();
            sb.Append("<ROOT>");
            foreach (Guid userid in userids)
                sb.AppendFormat("<USER ID=\"{0}\"/>",userid.ToString());
            sb.Append("</ROOT>");
            using (WMSDataContext db = new WMSDataContext())
            {
                return db.usp_Role_AddUsersToRole(sb.ToString(), roleid).ToList < usp_Role_AddUsersToRoleResult>();
            }
        }

        public static void CreateRole(string roleName, string desc)
        {
            System.Web.Security.Roles.CreateRole(roleName);
            using (WMSDataContext db = new WMSDataContext())
            {
                Role role = db.Roles.Single(r => r.RoleName == roleName);
                role.Description = desc;
                db.SubmitChanges();
            }
        }
        public static List<usp_Role_GetRolesResult> GetRoles(int pageIndex, int pageSize, string sortString, string whereString)
        {
            using (WMSDataContext db = new WMSDataContext())
            {
                return db.usp_Role_GetRoles(sortString, pageSize, pageIndex, whereString).ToList<usp_Role_GetRolesResult>();
            }
        }
    }
}
